package com.c.yinyuezhushou.Fragment;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.c.yinyuezhushou.Adapter.SongListAdapter;
import com.c.yinyuezhushou.Dialog.MyDialog;
import com.c.yinyuezhushou.Dialog.SongListDialog;
import com.c.yinyuezhushou.Entity.SongListEntity;
import com.c.yinyuezhushou.Interface.MyInterface;
import com.c.yinyuezhushou.MRetrofit.MusicItem;
import com.c.yinyuezhushou.MRetrofit.getSongDetail;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.MyTool.Immersed;
import com.c.yinyuezhushou.R;
import com.c.yinyuezhushou.Service.MusicService;
import com.c.yinyuezhushou.Sql.MyDButil;
import com.c.yinyuezhushou.Ui.ListActivity;

import java.util.ArrayList;
import java.util.List;

public class MineFragment extends Fragment {
    private View rootView;
    private MusicService musicService;
    private MyApplication myApplication;
    private MyDButil myDButil;
    private RelativeLayout relativeLayout;
    private RelativeLayout relativeLayout1;
    private RelativeLayout relativeLayout2;
    private RelativeLayout relativeLayout3;
    private RelativeLayout relativeLayout4;
    private RelativeLayout relativeLayout5;
    private RecyclerView recyclerView;
    private List<SongListEntity> list;
    private SongListAdapter songListAdapter;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_mine, container, false);
        relativeLayout = rootView.findViewById(R.id.fm_relayout);
        relativeLayout1 = rootView.findViewById(R.id.fm_re1);
        relativeLayout2 = rootView.findViewById(R.id.fm_re2);
        relativeLayout3 = rootView.findViewById(R.id.fm_re3);
        relativeLayout4 = rootView.findViewById(R.id.fm_re4);
        relativeLayout5 = rootView.findViewById(R.id.fm_re5);
        recyclerView = rootView.findViewById(R.id.fm_rec);

        myApplication = (MyApplication) getActivity().getApplicationContext();
        myDButil = myApplication.getMyDButil();

        musicService = myApplication.getMusicService();
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Immersed.setImmersed(relativeLayout,getActivity(),0);//设置状态栏

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);

        setRec();

        relativeLayout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myApplication.setFragment(new DownFragment());
                Intent intent = new Intent(getActivity(), ListActivity.class);
                intent.putExtra("title","下载管理");
                getActivity().startActivity(intent);
            }
        });
        relativeLayout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myApplication.setFragment(new LoveHistoryFragment(myDButil.getMusicItemTable("LoveMusicItem"),null));
                Intent intent = new Intent(getActivity(), ListActivity.class);
                intent.putExtra("title","我喜欢的");
                getActivity().startActivity(intent);
            }
        });
        relativeLayout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myApplication.setFragment(new LoveHistoryFragment(myDButil.getMusicItemTable("LsMusicItem"), new MyInterface() {
                    @Override
                    public void con() {

                    }

                    @Override
                    public void can() {

                    }

                    @Override
                    public void musicItem(MusicItem musicItem) {
                        MyDialog myDialog = new MyDialog(myApplication.getFragment().getContext(), true, null, "是否移除？","", new MyInterface() {
                            @Override
                            public void con() {
                                if (myApplication.getMyDButil().removeMusicItemTable(musicItem,"LsMusicItem")){
                                    list.remove(musicItem);
                                    Toast.makeText(myApplication, "移除成功", Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(myApplication, "移除失败", Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void can() {

                            }

                            @Override
                            public void musicItem(MusicItem musicItem) {

                            }
                        });
                        myDialog.show();
                    }
                }));
                Intent intent = new Intent(getActivity(), ListActivity.class);
                intent.putExtra("title","播放记录");
                getActivity().startActivity(intent);
            }
        });
        relativeLayout4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myApplication.setFragment(new SetupFragment());
                Intent intent = new Intent(getActivity(), ListActivity.class);
                intent.putExtra("title","设置");
                getActivity().startActivity(intent);
            }
        });
        relativeLayout5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SongListDialog songListDialog = new SongListDialog(getContext(),true,null,MineFragment.this);
                songListDialog.show();
            }
        });
    }

    public void setRec(String imageurl,String name){
        SongListEntity songListEntity = new SongListEntity();
        songListEntity.setImageurl(imageurl);
        songListEntity.setListname(name);
        list.add(songListEntity);
        if (songListAdapter!=null){
            songListAdapter.notifyDataSetChanged();
        }
    }

    public void setRec(){
        list = myDButil.getSongList();
        songListAdapter = new SongListAdapter(list,myApplication,this);
        recyclerView.setAdapter(songListAdapter);
    }

    public MyDButil getMyDButil() {
        return myDButil;
    }

    public void removeSongList(String name){
        if (myDButil.removeSongList(name)){
            Toast.makeText(myApplication, "删除成功！！", Toast.LENGTH_SHORT).show();
            setRec();
        }
    }


    public void setSongList(String s,String name,getSongDetail getSongDetail){
        for (getSongDetail.SongsBean songDetail : getSongDetail.getSongs()){
            myDButil.setSongListMusicItem(songDetail,name);
        }

        if (myDButil.setSongList(name,getSongDetail.getSongs().get(0).getAl().getPicUrl()+"?imageView=1&thumbnail=400z400&type=webp&quality=80")){
            this.setRec(getSongDetail.getSongs().get(0).getAl().getPicUrl()+"?imageView=1&thumbnail=400z400&type=webp&quality=80",name);
            Toast.makeText(myApplication, "添加成功", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(myApplication, "添加失败", Toast.LENGTH_SHORT).show();
        }
    }

    public void setFSongList(String name){
        if (myDButil.setSongList(name,"")){
            Toast.makeText(myApplication, "创建成功", Toast.LENGTH_SHORT).show();
        }
        setRec();
    }

}
